import _city from "../../../cache/_city";
import _location from "../../../cache/_location";
import _object from "../../../utils/_object";
import _layer from "../../../common/_layer";
import lib_city from "../../../api/lib_city";
import _activity from "../../../cache/_activity";


Page({
  /** 
   * 页面的初始数据 
   */
  data: {
    loading: false,
    locate_city: '', //定位城市 存储
    citys: [],
    currProvince: '',
    currIndexCity: 0,
    currCity: '',

    activity_id: '',
    tids:0,
    clickid: ''

  },

  onLoad: function (options) {

    if (_object._length(options) > 0 && options.hasOwnProperty('city')) {
      //设置项目缓存
      this.setData({
        currCity: options.city
      })
    }
  },


  onShow() {
    // 城市切换不存储
    this.setData({
      locate_city: _location.get()
    })
    //activity信息
    let activity = _activity.get();
    if(activity && _object._length(activity) > 0) {
        this.setData({
          activity_id: activity.activity_id,
          tids: activity.tids,
          clickid: activity.clickid
        })
    }

    // 城市列表
    this.init_city();

  },

  // 高亮城市
  init_city() {

    this.setData({
      citys: getApp().single_globalData.citys
    })

    this.data.citys.forEach((item, index) => {
      item.children.forEach((v, t) => {
        if (v.name == this.data.currCity) {
          this.setData({
            currProvince: item.name,
            currCity: v.name,
            currIndexCity: index
          })
        }
      })
    })

  },
  //重新定位
  refresh() {
    this.getLocation()
  },

  //定位
  getLocation() {
    tt.getLocation({
      success: (res) => {
        if (res.errMsg == 'getLocation:ok') {
          _location.set(res.city)
          this.setData({ loading: true })
          setTimeout(() => {
            this.setData({
              loading: false,
              locate_city: res.city
            })
          }, 500)
        }
      },
      fail: (res) => {
        this.userSetting()
      },
      complete: (res) => {
        // console.log(res, '===');
      }
    });

  },
  //用户授权
  userSetting() {
    let that = this;
    tt.getSetting({
      success: (res) => {
        let authSetting = res.authSetting
        if (authSetting['scope.userLocation']) {
          // 已授权
        } else {
          tt.showModal({
            title: '您未开启地理位置授权',
            content: '前往开启系统地理定位',
            success: res => {
              if (res.confirm) {
                tt.openSetting({
                  success: (result) => {
                    that.getLocation()
                  },
                });
              }
            }
          })
        }
      },
      fail: (res) => {
        console.log(res, '+++');
      },
    });
  },

  // 切换
  clkProvince(e) {
    const { index, item } = e.currentTarget.dataset
    this.setData({
      currIndexCity: index,
      currProvince: item.name
    })
  },
  clkCity(e) {
    this.setData({ currCity: e.currentTarget.dataset.item });
    //存city
    _city.set(this.data.currCity);

    //
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页面
    var prevPage = pages[pages.length - 2];

    prevPage.setData({
      city: this.data.currCity,
    })
    tt.navigateBack({
      delta: 1,
    })
  },

  //分享
  async onShareAppMessage(option) {

    const { activity_id, tids, clickid } = this.data

    return {
      title: '查询底价',
      path: `/pages/single/list/list?activity_id=${activity_id}&tids=${tids}&clickid=${clickid}`, // ?后面的参数会在转发页面打开时传入onLoad方法
      imageUrl: '/assets/image/banner.png', // 支持本地或远程图片，默认是小程序 icon
      success(res) {
        console.log('分享成功', res);
      },
      fail() {
        console.log("转发发布器调起失败");
      },
    };
  },

})
